# 该代码对答案进行挨句的bleu的计算
# 需要的语料格式：reference:每一行对应一个candidates，可能有多句references，是个list，每个refernence之中的词之间用空格隔开
# 待检测的candiates：每一行就一条，词之间空格隔开


# 这个文件有两个bleu需要被计算，一个是正常的词bleu，一个是按照字的bleu，词bleu 直接用nltk的就可以了,哦，字也是
from nltk.translate.bleu_score import sentence_bleu

# 这个函数读取reference、candidate文件并计算bleu
def bleu_score(candidate_file,reference_file):
    with open(candidate_file) as cadidates_f:
        with open(reference_file) as reference_f:
            cadidates_all = cadidates_f.readlines()
            reference_all = reference_f.readlines()
            if len(reference_all) != len(cadidates_all):
                print("reference和cadidates的数目不一致，请检查文件")
            all_score = []
            # nltk计算bleu需要的形式
            # reference = [['this', 'is', 'a', 'test'], ['this', 'is' 'test']]
            # candidate = ['this', 'is', 'a', 'test']
            for reference_line, candidate_line in zip(reference_all,cadidates_all):
                # reference_line 是reference的一个list
                # cadidate_line 是一句话
                reference = [sentence.strip().split() for sentence in eval(reference_line)]
                candidate = candidate_line.strip().split()
                score = sentence_bleu(reference,candidate)
                print("当前的reference:",reference)
                print("当前的candidate",candidate)
                print("bleu score:",score)
                all_score.append(score)
            return all_score


# 这个函数读取candidates的文件并进行diversity的计算
def diversity_score(candidate_file,reference_file):
    with open(candidate_file) as cadidates_f:
        cadidates_all = cadidates_f.readlines()
        all_words = []
        intra_diversity_list = []
        for candidate_line in cadidates_all:
            word_list = candidate_line.strip().split()
            word_set = set(word_list)

            intra_diversity = len(word_set)/len(word_list)
            intra_diversity_list.append(intra_diversity)

            all_words = all_words+word_list
        all_words_set = set(all_words)
        inter_diversity = len(all_words_set)/len(all_words)
    return inter_diversity,intra_diversity_list



